#include "Player.h"
#include "Prop.h"
#include <stdio.h>
#include <stdlib.h>
#include <DynamicArray.h>


void InitPlayer(player *p)
{
    p->hp = 100;
    p->attack = 5;
    p->x = 0;
    p->y = 0;
    p->symbol = 'X';
    p->exp = 0;
    p->level = 0;
    p->golden = 0;

    InitDynamicArray(&p->bag);

}

void PlayerMove(player *p ,char (*map)[MAPSIZE])
{
    //将原来的位置清空
    map[p->x][p->y] = '-'; 

    char choice = 0;
    printf("请选择玩家移动方向(⬆(w), ⬅ (a), ⬇(s), ➡ (d), 进入商店🛒(p), 打开背包(b):\n");
    scanf(" %c",&choice);

    switch(choice)
    {
        case 'w':p->x--;
                if(p->x < 0)
                    p->x = 0;
                break;
        case 's':p->x++;
                if(p->x >= MAPSIZE)
                    p->x = MAPSIZE-1;
                break;
        case 'a':p->y--;
                if(p->y < 0)
                    p->y = 0;
                break;
        case 'd':p->y++;
                if(p->y >= MAPSIZE)
                    p->y = MAPSIZE-1;
                break;
        case 'p':
                sleep(1);
                ShopInterFace(p);
                break;
        case 'b':
                sleep(1);
                ShowBag(p);
                break;
        default:break;
    }

    //附上新位置
    map[p->x][p->y] = p->symbol;
}


void PrintPlayer(player *p)
{
    printf("玩家当前状态：等级⬆️ :  |%d|   血量🤍 :|%3d| 攻击力⚔️ :|%d| 金钱💰:|%d|\n",p->level,p->hp,p->attack,p->golden);
}



void LevelUp(player *p)
{
    if(p->exp >= (p->level+1) *50)
    {
        p->level++;
        printf("恭喜升级⬆️到%d级!\n",p->level);

        p->hp = 100;
        p->attack += p->attack *0.5;
    }
}

void ShopInterFace(player *p)
{
    // while(1)
    // {
        system("clear");
        printf("---------------欢迎来到商店🛒! ψ(｀∇´)ψ-----------------\n");
        printf("以下是你可以购买的商品！\n");
        printf("1、村里最好的剑🔪       (攻击力⚔️  +5):  ￥5\n");
        printf("2、小血瓶💗             (血量🤍   +5):  ￥2\n");
        printf("3、大血瓶💕             (血量🤍   +10): ￥4\n");
        printf("\n");
        printf("---------------------以上为商品目录----------------------\n");
        printf("                                            q:退出商店🛒\n");
        printf("                                       你当前拥有金币💰%d\n",p->golden);
        printf("请做出你的选择: \n");
  
        int choice = 0;
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                if(p->golden < 5)
                {
                    sleep(1);
                    printf("余额不足，购买武器🔪失败！\n");
                    sleep(1);
                    break;
                }
                printf("购买武器🔪成功！\n");
                p->bag.InsertArrayTail(&p->bag,CreateProp(Weapon,CreateWeapon("村里最好的剑🗡",5,-1)));
                p->golden -= 5;
                break;
            case 2:
                if(p->golden < 2)
                {
                    printf("余额💰不足，购买失败！\n");
                    break;
                }
                p->golden -= 2;
                p->hp += 5; 
            case 3:
                if(p->golden < 4)
                {
                    printf("余额💰不足，购买失败！\n");
                    break;
                }
                p->golden -= 4;
                p->hp += 10; 
            default:
                    break;
        }
    //}
}

void ShowProp(void *ptr)
{
    prop *p = (prop*)ptr;
    if(p->type == Weapon)
    {
        weapon *w = (weapon*)p->ptr;
        printf("武器⚔ 名字:|%s| 攻击力⚔️ :|%d|\n",w->name.string,w->attack);
    }
}

void ShowBag(player *p)
{
    system("clear");
    printf("-----------------欢迎打开背包界面-------------------\n");
    printf("以下是你拥有的物品:\n");
    if(p->bag.len == 0)
    {
        printf("抱歉！你还没有物品！\n");

    }
    else
    {
        p->bag.DisplayDynamicArray(&p->bag,ShowProp);
    }
    printf("---------------------------------------------------\n");
    sleep(1);
}

